package servcie

import (
	mes "ez/apps/mes/document"
	"gitee.com/dreamwood/ez-go/ss"
	"strings"
)

type QrInfo struct {
	Code string `json:"code"`
	Plan string `json:"plan"`
	Sort string `json:"sort"`
	Rnd  string `json:"rnd"`
}

func NewQrCode(code string) *QrInfo {
	arr := strings.Split(code, ".")
	qi := new(QrInfo)
	qi.Code = code
	if len(arr) > 0 {
		qi.Plan = arr[0]
	}
	if len(arr) > 1 {
		qi.Rnd = arr[1]
	}
	if len(arr) > 2 {
		qi.Sort = arr[2]
	}
	return qi
}

func (qi *QrInfo) GetProduct() string {
	return qi.GetPlan().Product
}

func (qi *QrInfo) GetPlan() *mes.Plan {
	crud := mes.NewPlanCrud()
	plan, _ := crud.FindOneBy(ss.M{"sn": qi.Plan}, nil)
	return plan
}

func (qi *QrInfo) GetFlowOrCreateEmpty() *mes.Flow {
	crud := mes.NewFlowCrud()
	flow, _ := crud.FindOneBy(ss.M{"sn": qi.Code}, nil)
	if flow.Id == 0 {
		flow.Sn = qi.Code
		//获取计划
		plan := qi.GetPlan()
		flow.Plan = qi.Plan
		flow.TemplateId = plan.TemplateId
		_ = flow.Save()
	} else {
		flow.LoadPapers()
	}
	return flow
}
